<template>
  <div class="pugtablebox">
    <div class="mb-3 flex justify-between">
      <div>
        <el-button size="small" type="success" @click="handleAdd" :icon="CirclePlus">新增</el-button>
        <el-button size="small" type="danger" :icon="Delete">批量删除</el-button>
      </div>
      <div>
        <el-input v-model="keyword" class="w-50 m-2" size="small" placeholder="请输入搜索关键词" :suffix-icon="Search" />
        <el-button size="small" type="success" :icon="Search">搜索</el-button>
      </div>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="Date">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-icon>
              <timer />
            </el-icon>
            <span style="margin-left: 10px">{{ scope.row.date }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="Name">
        <template #default="scope">
          <el-popover effect="light" trigger="hover" placement="top" width="auto">
            <template #default>
              <div>name: {{ scope.row.name }}</div>
              <div>address: {{ scope.row.address }}</div>
            </template>
            <template #reference>
              <el-tag>{{ scope.row.name }}</el-tag>
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="Operations">
        <template #default="scope">
          {{ scope.row.status }}
          <el-switch v-model="scope.row.status" class="ml-2" width="60px" inline-prompt :active-value="1"
            :inactive-value="0" active-text="发布" inactive-text="不发布" />
        </template>
      </el-table-column>
      <el-table-column label="Operations">
        <template #default="scope">
          <el-button size="small" :icon="Edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="small" :icon="Edit" @click="handlePermission(scope.$index, scope.row)">授权</el-button>
          <el-button size="small" type="danger" :icon="Delete" @click="handleDelete(scope.$index, scope.row)">Delete
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="justify-center mt-5" background layout="prev, pager, next" :total="1000" />
  </div>

  <pug-drawer ref="roleAddRef" title="角色管理" @submit="handleSubmit">
    <el-form :model="role" ref="roleFormRef" :rules="rules" label-width="80px" :inline="false" size="normal">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="role.name" placeholder="角色名称"></el-input>
      </el-form-item>
      <el-form-item label="角色描述" prop="desc">
        <el-input v-model="role.desc" placeholder="角色描述" type="textarea" :rows="15"></el-input>
      </el-form-item>
      <el-form-item label="角色状态" prop="status">
        <el-switch v-model="role.status" :active-value="1" :inactive-value="0">
        </el-switch>
      </el-form-item>
    </el-form>
  </pug-drawer>


  <pug-drawer ref="permissionRef" title="权限管理" @submit="handlePermissionSubmit">
    <el-tree-v2 ref="elTreeRef" node-key="id" :default-expanded-keys="defaultExpandedKeys" 
      :data="permissionList" :props="{label:'name',children:'children'}" show-checkbox 
      :height="treeHeight"
      :check-strictly="checkStrictly"
      @check="handleChecked">
        <template #default="{node,data}">
            <div class="flex item-center">
              <el-tag :type="data.menu==1?'info':''" size="small">
                {{data.menu==1?"权限":"菜单"}}
              </el-tag>
              <span class="ml-2 text-sm">{{data.name}}</span>
            </div>
        </template>
    </el-tree-v2>
  </pug-drawer>
</template>

<script setup>
import { Timer } from '@element-plus/icons-vue'
import { ref, reactive } from 'vue'
import { CirclePlus, Top, Bottom, Delete, Edit, Search, Share, Upload } from '@element-plus/icons-vue'
import navMenuService from '@/services/navmenu/NavMenuService';
import { toast,confirm } from '../../utils/utils';
import PugTableCommon from '@/api/PugTableCommon.js';

const {
  searchForm,
  resetSearchForm,
  tableData,
  pageNo,
  pageSize,
  total,
  pages,
  loadData
} = await PugTableCommon();  






const roleAddRef = ref(null);
const roleFormRef = ref(null);
const rules =  {
    name: [{
      required: true,
      message: '角色名称不能为空',
      trigger: 'blur'
    }]
  };
const role = reactive({
  role: "",
  desc: "",
  status: 0,
})

const handleAdd = () => {
  roleAddRef.value.open();
}

const handleSubmit = () => {
  roleFormRef.value.validate((valid) => {
      if(!valid){
        return;
      }
      roleAddRef.value.showLoading();
      alert("保存数据" + JSON.stringify(role))
  })
}


/*授权数据 */
/*授权 */
const permissionRef = ref(null);
const permissionList = ref([]);
const treeHeight = ref(200);
const roleId = ref(0);
const rolePermissionList = ref([]);
const elTreeRef = ref(null);
const defaultExpandedKeys = ref([]);
const checkStrictly = ref(false);
const handlePermission = async (index,item)=>{
  roleId.value = item.id;
  treeHeight.value = window.innerHeight - 180;
  checkStrictly.value = true;
  // 获取所有的权限
  const res = await navMenuService.getMenuList();
  defaultExpandedKeys.value = res.data.map(o=>o.id);
  permissionList.value = res.data;

  // 获取当前角色对应的权限信息--伪造数据
  rolePermissionList.value = res.data.map(o=>o.id);
  // setTimeout(() => {
  //    elTreeRef.value.setCheckedKeys(rolePermissionList.value);
  // }, 100);
  checkStrictly.value = false;
  permissionRef.value.open();
}

// 获取所有选中的tree节点
const handleChecked = (...e)=>{
  const {checkedKeys,halfCheckedKeys} = e[1];
  rolePermissionList.value = [...checkedKeys,...halfCheckedKeys];
}

const handlePermissionSubmit = ()=>{
   confirm("你确定授权吗?").then(res=>{
     console.log("保存的角色信息是：",roleId.value)
      console.log("保存的角色权限信息是：",rolePermissionList.value)
      toast("配置成功")
   })
}
</script>
